package com.grass.appointment.comment;

import android.content.Context;
import android.graphics.Color;
import android.text.SpannableString;
import android.text.style.ForegroundColorSpan;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.androidx.p013lv.base.BaseSdk;
import com.androidx.p013lv.base.glide.GlideUtils;
import com.androidx.p013lv.base.utils.ButtonUtil;
import com.androidx.p013lv.base.utils.SpUtils;
import com.androidx.p013lv.base.utils.TimeUtils;
import com.androidx.p013lv.base.utils.UiUtils;
import com.androidx.p013lv.base.view.CircleImageView;
import com.grass.appointment.C2694R;
import com.grass.appointment.bean.ReplyBean;
import com.like.LikeButton;
import com.like.OnLikeListener;
import java.util.ArrayList;
import java.util.List;

public class CommentVerticalLayout extends LinearLayout implements ViewGroup.OnHierarchyChangeListener {
    private int mCommentVerticalSpace;
    private LinearLayout.LayoutParams mLayoutParams;
    private OnTwoClickListener onTwoClickListener;
    private int parentId;
    private List<ReplyBean> replyList;
    private int userId;

    public interface OnTwoClickListener {
        void onTwoClick(ReplyBean replyBean, int i);
    }

    public void onChildViewAdded(View view, View view2) {
    }

    public void onChildViewRemoved(View view, View view2) {
    }

    public void setParentId(int i) {
        this.parentId = i;
    }

    public void setOnTwoClickListener(OnTwoClickListener onTwoClickListener2) {
        this.onTwoClickListener = onTwoClickListener2;
    }

    public void setUserId(int i) {
        this.userId = i;
    }

    public CommentVerticalLayout(Context context) {
        super(context);
        init();
    }

    public CommentVerticalLayout(Context context, AttributeSet attributeSet) {
        super(context, attributeSet);
        init();
    }

    public CommentVerticalLayout(Context context, AttributeSet attributeSet, int i) {
        super(context, attributeSet, i);
        init();
    }

    private void init() {
        setOrientation(1);
        this.mCommentVerticalSpace = UiUtils.dp2px(2.0f);
        setOnHierarchyChangeListener(this);
    }

    public void addCommentsWithLimit(List<ReplyBean> list, int i, boolean z) {
        if (list != null) {
            this.replyList = list;
            int childCount = getChildCount();
            if (!z && childCount > 0) {
                removeViewsInLayout(0, childCount);
            }
            int min = Math.min(i, list.size());
            int i2 = 0;
            while (i2 < min) {
                View childAt = i2 < childCount ? getChildAt(i2) : null;
                ReplyBean replyBean = list.get(i2);
                if (childAt == null) {
                    addViewInLayout(makeCommentItemView(replyBean, i2), i2, generateMarginLayoutParams(i2), true);
                } else {
                    addCommentItemView(childAt, replyBean, i2);
                }
                i2++;
            }
            requestLayout();
        }
    }

    private LinearLayout.LayoutParams generateMarginLayoutParams(int i) {
        if (this.mLayoutParams == null) {
            this.mLayoutParams = new LinearLayout.LayoutParams(-1, -2);
        }
        if (this.replyList != null && i > 0) {
            this.mLayoutParams.topMargin = (int) (((float) this.mCommentVerticalSpace) * 1.2f);
        }
        return this.mLayoutParams;
    }

    private void updateCommentData(View view, ReplyBean replyBean, int i) {
        bindViewData(view, replyBean);
    }

    private void addCommentItemView(View view, ReplyBean replyBean, int i) {
        addViewInLayout(makeCommentItemView(replyBean, i), i, generateMarginLayoutParams(i), true);
    }

    private View makeCommentItemView(ReplyBean replyBean, int i) {
        return makeContentView(replyBean, i);
    }

    private View makeContentView(final ReplyBean replyBean, int i) {
        View inflate = View.inflate(getContext(), C2694R.C2699layout.comment_item_two, null);
        inflate.setOnClickListener(new View.OnClickListener() {
            /* class com.grass.appointment.comment.CommentVerticalLayout.View$OnClickListenerC27841 */

            public void onClick(View view) {
                if (CommentVerticalLayout.this.onTwoClickListener != null) {
                    CommentVerticalLayout.this.onTwoClickListener.onTwoClick(replyBean, CommentVerticalLayout.this.parentId);
                }
            }
        });
        bindViewData(inflate, replyBean);
        return inflate;
    }

    private void bindViewData(View view, final ReplyBean replyBean) {
        TextView textView = (TextView) view.findViewById(C2694R.C2697id.tv_author);
        TextView textView2 = (TextView) view.findViewById(C2694R.C2697id.tv_content);
        TextView textView3 = (TextView) view.findViewById(C2694R.C2697id.tv_time);
        LikeButton likeButton = (LikeButton) view.findViewById(C2694R.C2697id.bt_isLike);
        final TextView textView4 = (TextView) view.findViewById(C2694R.C2697id.tv_like_num);
        GlideUtils.loadPicHead(SpUtils.getInstance().getString("domain") + replyBean.getUserLogo(), (CircleImageView) view.findViewById(C2694R.C2697id.img_head));
        ((TextView) view.findViewById(C2694R.C2697id.tv_name)).setText(replyBean.getUserNickName());
        if (replyBean.getReplyUserId() == null) {
            textView2.setText(replyBean.getContent());
        } else {
            SpannableString spannableString = new SpannableString("回复  " + replyBean.getReplyUserNickName() + ":  " + replyBean.getContent());
            spannableString.setSpan(new ForegroundColorSpan(Color.parseColor("#ffffff")), 4, replyBean.getReplyUserNickName().length() + 4, 33);
            textView2.setText(spannableString);
        }
        textView3.setText(TimeUtils.utc2Common(replyBean.getCreatedAt()));
        textView4.setText(UiUtils.num2str(replyBean.getLikeNum()));
        boolean z = false;
        if (this.userId == replyBean.getUserId()) {
            textView.setVisibility(0);
        } else {
            textView.setVisibility(8);
        }
        final int userId2 = SpUtils.getInstance().getUserInfo().getUserId();
        if (replyBean.getLikeUserIds() != null) {
            z = replyBean.getLikeUserIds().contains(Integer.valueOf(userId2));
        }
        if (z) {
            textView4.setTextColor(BaseSdk.getContext().getResources().getColor(C2694R.C2695color.E24339));
        } else {
            textView4.setTextColor(BaseSdk.getContext().getResources().getColor(C2694R.C2695color.white));
        }
        likeButton.setLiked(Boolean.valueOf(z));
        likeButton.setOnLikeListener(new OnLikeListener() {
            /* class com.grass.appointment.comment.CommentVerticalLayout.C27852 */

            @Override // com.like.OnLikeListener
            public void liked(LikeButton likeButton) {
                if (ButtonUtil.isFastDoubleClick(1000)) {
                    likeButton.setLiked(false);
                    return;
                }
                List<Integer> likeUserIds = replyBean.getLikeUserIds();
                if (likeUserIds == null) {
                    likeUserIds = new ArrayList<>();
                }
                likeUserIds.add(Integer.valueOf(userId2));
                replyBean.setLikeUserIds(likeUserIds);
                ReplyBean replyBean = replyBean;
                replyBean.setLikeNum(replyBean.getLikeNum() + 1);
                textView4.setText(UiUtils.num2str(replyBean.getLikeNum()));
                textView4.setTextColor(BaseSdk.getContext().getResources().getColor(C2694R.C2695color.E24339));
                CommentHttpUtils.replyLike(replyBean, CommentVerticalLayout.this.parentId);
            }

            @Override // com.like.OnLikeListener
            public void unLiked(LikeButton likeButton) {
                if (ButtonUtil.isFastDoubleClick(1000)) {
                    likeButton.setLiked(true);
                    return;
                }
                if (replyBean.getLikeNum() >= 1) {
                    ReplyBean replyBean = replyBean;
                    replyBean.setLikeNum(replyBean.getLikeNum() - 1);
                }
                List<Integer> likeUserIds = replyBean.getLikeUserIds();
                if (likeUserIds != null) {
                    for (int i = 0; i < likeUserIds.size(); i++) {
                        if (userId2 == likeUserIds.get(i).intValue()) {
                            likeUserIds.remove(i);
                        }
                    }
                }
                textView4.setText(UiUtils.num2str(replyBean.getLikeNum()));
                textView4.setTextColor(BaseSdk.getContext().getResources().getColor(C2694R.C2695color.white));
                CommentHttpUtils.replyUnlike(replyBean, CommentVerticalLayout.this.parentId);
            }
        });
    }
}
